***Last Modified:  08/29/12                                        ***;
** Modified by Kirk White 08/29/12 to work in my project space and ***;
** just use the BEA data for 2002-2007.                            ***;
** I also modified the labels to be consistent with the data that I***;
** downloaded from BEA.                                            ***;
**********************************************************************;
***This program reads in the NAICS-based BEA data for              ***;
***NKCEQ, NKCST, GKHEQ, GKHST.                                     ***;
**********************************************************************;
***Output:                                                         ***;
*** bea.bea_naics.sas7bdat                          ***;
**********************************************************************;

options ls=150 ps=max;

%include "ASMimplibs.sas";

***START MACROS***;

*Macro to read in csv files;

%macro c(var, y1, y2);

proc import datafile="&var..csv"
	dbms=csv out=&var replace;

data &var;
 set &var;
 if line ne 999;	

%*Create dataset with line and NAICS vars;

data n&var;
 set &var (keep = line naics_bea industry_desc_bea);

proc sort data=n&var;
 by line;


%*Create empty dataset to add to later;

data f&var (keep = line &var year);
 set &var;
 line = 998;
 &var = .;
 year = 9998;
 if line = 998;


%do i=&y1 %to &y2;

data &var&i (keep = line &var year);
 set &var;
 line = 999;
 &var = .;
 year = 9999;
 if line = 999;

 %do j=11 %to 32;
 	data &var&i&j (keep = &var year line);
  	set &var;
	 if line = &j;
	 year = &i;
	 &var = y&i;
 	 data &var&i;
 	  set &var&i &var&i&j;
 %end;

 data &var&i;
  set &var&i;
  if line ne 999;
   data f&var;
   set f&var &var&i;

%end;

data f&var;
	set f&var;
	if line ne 998;

proc sort data=f&var;
	by line year;

%*proc print data=f&var;
%*	var line year &var;
%*	title1 "f&var";

%mend c;


***START PROGRAM***;

%c(nkceq, 2002, 2007)
%c(nkcst, 2002, 2007)
%c(gkheq, 2002, 2007)
%c(gkhst, 2002, 2007)


data bea;
 merge fnkceq fnkcst fgkheq fgkhst;
 by line year;

data bea.bea_naics;
 merge bea nnkceq;
 by line;

 label line = 'BEA line number from original Excel file';
 label NAICS_BEA = 'NAICS codes corresponding to BEA description (Character, length 22)';
 label Industry_Desc_BEA = 'BEA Industry Description';
 label gkheq = 'Historical-cost net stock of private equipment and software, yearend estimates ($1B) [BEA Table 3.3E, revised August 14, 2012]';
 label gkhst = 'Historical-cost net stock of private structures, yearend estimates ($1B) [BEA Table 3.3S, revised August 14, 2012]';
 label nkceq = 'Current-cost net stock of private equipment and software, yearend estimates ($1B) [BEA Table 3.1E, revised August 14, 2012]';
 label nkcst = 'Current-cost net stock of private structures, yearend estimates ($1B) [BEA Table 3.1S, revised August 14, 2012]';
 label year = 'Year (4-digit)';

proc contents data=bea.bea_naics;
 title1 "bea.bea_naics";

proc print data=bea.bea_naics;
 var line year naics_bea gkheq gkhst nkceq nkcst industry:;
 title1 "bea.bea_naics";
run;
 
